package com.dreamer.view.pyramid;

import com.dreamer.domain.pyramid.CashWithdrawal;
import com.dreamer.service.pyramid.CashWithdrawalHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ps.mx.otter.utils.message.Message;

import java.util.Date;
import java.util.Optional;

@RestController
@RequestMapping("/cash")
public class CashController {

    @Autowired
    private CashWithdrawalHandler cashWithdrawalHandler;

    @RequestMapping(value = "/edit.json", method = RequestMethod.POST)
    public Message edit(@ModelAttribute("parameter") CashWithdrawal cashWithdrawal) {
        if(cashWithdrawal.getStatus().getName() == "已通过" || cashWithdrawal.getStatus().getName() == "未通过"){
            cashWithdrawal.setName("管理员");
            cashWithdrawal.setHandleTime(new Date());
        }
        CashWithdrawal cashWithdrawal1 = cashWithdrawalHandler.saveOrupdate(cashWithdrawal);
        if(cashWithdrawal1 != null){
            return Message.createSuccessMessage();
        }else{
            return Message.createFailedMessage();
        }
    }

    @ModelAttribute("parameter")
    public CashWithdrawal preprocess(@RequestParam("id") Optional<Integer> id) {
        CashWithdrawal cashWithdrawal = new CashWithdrawal();
        if (id.isPresent()) {
            cashWithdrawal = cashWithdrawalHandler.findById(id.get());
        }
        return cashWithdrawal;
    }

}
